package com.miplus.ccrm.core.service.conf;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.core.controller.vo.LabelResVo;
import com.miplus.ccrm.core.controller.vo.conf.CmTrunkReqVo;
import com.miplus.ccrm.core.pojo.CmTrunk;
import com.miplus.ccrm.core.mapper.CmTrunkMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wangzl
 * @description 针对表【cm_trunk】的数据库操作Service实现
 * @createDate 2025-07-12 14:26:17
 */
@Service
public class CmTrunkServiceImpl extends ServiceImpl<CmTrunkMapper, CmTrunk>
        implements CmTrunkService{
    @Override
    public IPage<CmTrunk> selectCmTrunkList(CmTrunkReqVo paras)
    {
        return baseMapper.selectPage(new Page<>(paras.getPageNum(),paras.getPageSize()),
                new LambdaQueryWrapper<CmTrunk>()
                        .eq(ObjectUtils.isNotNull(paras.getStatus()),CmTrunk::getStatus,paras.getStatus())
                        .eq(StringUtils.isNotEmpty(paras.getSymbolphone()), CmTrunk::getSymbolphone, paras.getSymbolphone())
                        .like(StringUtils.isNotEmpty(paras.getName()),CmTrunk::getName, paras.getName())
                        .ge(StringUtils.isNotEmpty(paras.getCrdt1()),CmTrunk::getCrdt,paras.getCrdt1())
                        .le(StringUtils.isNotEmpty(paras.getCrdt2()),CmTrunk::getCrdt,paras.getCrdt2())
                        .orderByDesc(CmTrunk::getCrdt)

        );

    }
    @Override
    public CmdResVo addCmTrunk(String usrid, CmTrunk cmtrunk)
    {
        cmtrunk.setCrdt(new Date());
        cmtrunk.setCrusr(usrid);
        save(cmtrunk);
        return new CmdResVo(cmtrunk.getTrunkid(),"创建OK。");
    }
    @Override
    public CmdResVo updateCmTrunk(String usrid, CmTrunk cmtrunk)
    {
        if(cmtrunk == null || ObjectUtils.isNull(cmtrunk.getTrunkid()))
            return new CmdResVo(-1,"参数不正确");
        CmTrunk ct1 = getById(cmtrunk.getTrunkid());
        if( ct1 == null)
            return new CmdResVo(-1,"当前线路不存在");
        cmtrunk.setMddt(new Date());
        cmtrunk.setMdusr(usrid);

        updateById(cmtrunk);
        return new CmdResVo(0,"更新OK");
    }
    @Override
    public CmdResVo deleteCmTrunk(String usrid, Integer cmtrunkId)
    {
        if(ObjectUtils.isNull(cmtrunkId))
            return new CmdResVo(-1,"参数不正确");
        CmTrunk cmTrunk = getById(cmtrunkId);
        if( cmTrunk == null)
            return new CmdResVo(-1,"当前线路不存在");
        removeById(cmTrunk);
        return new CmdResVo(0,"删除成功");
    }
    @Override
    public List<LabelResVo> getCmTrunkList()
    {
        List<CmTrunk> ct = baseMapper.selectList( new LambdaQueryWrapper<CmTrunk>()
                .eq(CmTrunk::getStatus,1)
        );
        List<LabelResVo> result = new ArrayList<>();
        if(ct == null || ct.size() == 0)
            return result;
        for( CmTrunk x : ct)
        {
            LabelResVo v = new LabelResVo();
            v.setLabel(x.getName()+"(" + x.getSymbolphone() + ")");
            v.setValue(String.valueOf(x.getTrunkid()));
            result.add(v);
        }
        return result;
    }

}




